package com.yanqu.road.server.cmd.union;

import com.yanqu.road.server.pb.UnionPb;
import com.yanqu.road.entity.enums.eUnionNoticeType;
import com.yanqu.road.entity.union.UnionInfo;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.server.manger.rank.RankMgr;
import com.yanqu.road.server.manger.union.UnionActivityMgr;
import com.yanqu.road.server.manger.union.UnionMgr;
import com.yanqu.road.server.manger.union.UnionNoticeMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.utils.string.StringUtils;

//@Cmd(code = Protocol.S_UNION_CHANGE_FLAG, desc = "修改联盟旗帜")
public class ChangeUnionFlagCmd extends AbstractCommand {

    private int unionFlag;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        UnionProto.UnionModifyFlagReqMsg reqMsg = UnionProto.UnionModifyFlagReqMsg.parseFrom(packet.getBytes());
        unionFlag = reqMsg.getUnionFlag();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        String unionUid = player.getUserInfo().getUnionUid();
        if(StringUtils.isNullOrEmpty(unionUid)){
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }
        UnionInfo unionInfo = UnionMgr.getUnionInfo(unionUid);
        if(null == unionInfo){
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }
        if (unionInfo.getMasterUserId() != player.getUserId()) {//权限不足
            ret = GameErrorCode.E_UNION_POSITION_NO_ENOUGH;
            return;
        }
        if(!UnionMgr.hasUnionFlag(unionUid,unionFlag)){
            ret = GameErrorCode.E_UNION_FLAG_NO_FOUND;
            return;
        }
        synchronized (unionInfo) {
            long nowTime = System.currentTimeMillis() / 1000;
            if (nowTime - unionInfo.getFlagModifyTime() < 3600) {
                ret = GameErrorCode.E_UNION_FLAG_CHANGE_TIME_ONE_HOUR;
                return;
            }

            int preUnionFlag = unionInfo.getUnionFlag();
            if (ConfigMgr.hasUnionFlag(preUnionFlag)) {
                //如果是默认的会旗，则记录
                unionInfo.setPreUnionFlag(preUnionFlag);
            }
            unionInfo.setUnionFlag(this.unionFlag);
            unionInfo.setFlagModifyTime(nowTime);
        }
        //增加联盟日志
        UnionNoticeMgr.addNotice(player, unionUid, eUnionNoticeType.UnionModify.getValue(), "");
        RankMgr.changeUnionRank(unionInfo);
        UnionActivityMgr.changeUnionInfo(unionInfo);
    }

    @Override
    public void sendData(GamePlayer player) {
        UnionProto.UnionChangeRandomJoinRespMsg.Builder respMsg = UnionProto.UnionChangeRandomJoinRespMsg.newBuilder();
        respMsg.setRet(ret);
        if (ret == 0) {
            UnionInfo unionInfo = UnionMgr.getUnionInfo(player.getUserInfo().getUnionUid());
            UnionProto.UnionTempMsg.Builder unionMsg = UnionPb.parseUnionTempMsg(unionInfo);
            if (null != unionMsg) {
                respMsg.setUnion(unionMsg);
            }
        }
        player.sendPacket(Protocol.U_UNION_CHANGE_FLAG, respMsg);
    }
}
